package test.sunyu.tools.hbase;

import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.PrefixFilter;
import org.apache.hadoop.hbase.util.Bytes;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sunyu.tools.common.FileTools;
import sunyu.tools.hbase.HbaseTools;

import java.io.IOException;

public class CountTask implements Runnable {
    private static final Logger logger = LoggerFactory.getLogger(Thread.currentThread().getClass());

    String did;
    String filePath;
    FileTools fileTools;
    HbaseTools hbaseTools;

    public CountTask(String did, String filePath, FileTools fileTools, HbaseTools hbaseTools) {
        this.did = did;
        this.filePath = filePath;
        this.fileTools = fileTools;
        this.hbaseTools = hbaseTools;
    }

    @Override
    public void run() {
        System.out.println(Thread.currentThread().getName() + ":处理" + did);
        FilterList filterList = new FilterList();
        Filter preFilter = new PrefixFilter(Bytes.toBytes(did));
        filterList.addFilter(preFilter);
        try {
            String msg = did + ":" + hbaseTools.count("bc_msg", filterList);
            System.out.println(msg);
            fileTools.appendContent(filePath, msg, true);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
